/*
 * Copyright (c) 2007 Compl Yue Still, All Rights Reserved.
 */

package hbi.domain.as3
{
	import flash.utils.*;
	
	import hbi.as3.utils.IFlushableOutput;
	import hbi.domain.DomainController;
	import hbi.domain.Gateway;
	import hbi.wire.as3.guest.GuestEnd;
	
	public class AS3Gateway implements Gateway
	{
		
		private var _localDomain:DomainController;
		private var _guestEnd:GuestEnd;
		
		public function AS3Gateway
			(localDomain:DomainController, output:IFlushableOutput)
		{
			this._localDomain = localDomain;
			this._guestEnd = new GuestEnd(output);
		}
		
		public function get localDomain():DomainController
		{
			return _localDomain;
		}
		
		/**
		 * @param script a function that takes a TaskAgent object
		 */
		public function sendTaskAgent
			(agentClass:Class, script:Function=null):void
		{
			_guestEnd.startAgent(agentClass);
			if(script != null)
			{
				var stub:LocalAgentStub = new LocalAgentStub(_guestEnd);
				script(stub);
			}
			_guestEnd.finishAgent();
		}

	}
}
